home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / cgi_buffer.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  4.4 KB  |  115 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. ''' 
  5. cgi_buffer is a library that may be used to improve performance of CGI
  6. scripts in some circumstances, by applying HTTP mechanisms that are
  7. typically not supported by them.
  8.  
  9. For more information:
  10.   http://www.mnot.net/cgi_buffer/
  11. '''
  12. __version__ = '0.3'
  13. error = 'cgi_buffer error'
  14. generate_etag = 1
  15. compress_content = 1
  16. import sys
  17. import cStringIO
  18. import httpheaders
  19. import httpheadertypes
  20. import os
  21. import base64
  22. import md5
  23. import time
  24. from copy import copy
  25. from string import index, lower
  26.  
  27. try:
  28.     import zlib
  29.     import gzip
  30. except:
  31.     zlib = None
  32.  
  33.  
  34. def init():
  35.     ''' Some slight setup '''
  36.     sys.stdout = cStringIO.StringIO()
  37.     sys.exitfunc = cleanup
  38.  
  39.  
  40. def cleanup():
  41.     '''
  42. \tGather the contents of sys.stdout, do appropriate processing, and print
  43. \tthe result to the *true* stdout.
  44. \t'''
  45.     env = os.environ
  46.     f = sys.stdout.tell()
  47.     sys.stdout.seek(0)
  48.     g = sys.stdout.read()
  49.     sys.stdout = sys.__stdout__
  50.     
  51.     try:
  52.         i = index(g, '\n\n')
  53.     except ValueError:
  54.         raise error, "Can't find end of HTTP Headers."
  55.  
  56.     
  57.     try:
  58.         headers = httpheaders.DemandHeaders(g[:i])
  59.     except StandardError:
  60.         why = None
  61.         raise error, 'HTTP header parsing error: %s' % why
  62.  
  63.     body = g[i + 2:]
  64.     if env.has_key('HTTP_ACCEPT_ENCODING') and compress_content and zlib and headers.content_type[:4] == 'text':
  65.         accept_encoding = httpheadertypes.QValList(env['HTTP_ACCEPT_ENCODING'])
  66.         for encoding in accept_encoding:
  67.             if encoding == 'gzip' or encoding == 'x-gzip':
  68.                 headers.content_encoding.append(encoding)
  69.                 headers.vary.append('Accept-Encoding')
  70.                 body = _gzip_body(body)
  71.                 break
  72.                 continue
  73.         
  74.     
  75.     if generate_etag:
  76.         etag = base64.encodestring(md5.new(body).digest())[:-1]
  77.         headers.etag = etag
  78.         if env.has_key('HTTP_IF_NONE_MATCH'):
  79.             validators = httpheadertypes.ETagList(env['HTTP_IF_NONE_MATCH'])
  80.             if etag in validators:
  81.                 print 'Status: 304 Not Modified'
  82.                 headers.send_entity = 0
  83.                 print headers
  84.                 return None
  85.             
  86.         
  87.     
  88.     headers.content_length = len(body)
  89.     sys.stdout.write('%s\r\n%s' % (headers, body))
  90.  
  91.  
  92. def run_program(program_name):
  93.     ''' simple wrapper to run a program '''
  94.     print os.popen(program_name).read()
  95.  
  96.  
  97. def _gzip_body(i):
  98.     tmptime = time.time
  99.     
  100.     time.time = lambda a = None: 0
  101.     sb = cStringIO.StringIO()
  102.     gb = gzip.GzipFile(mode = 'wb', fileobj = sb, compresslevel = 2)
  103.     gb.write(i)
  104.     gb.close()
  105.     time.time = tmptime
  106.     sb.seek(0)
  107.     o = sb.read()
  108.     sb.close()
  109.     return o
  110.  
  111. if __name__ == '__main__':
  112.     pass
  113. else:
  114.     init()
  115.